package cn.school.mybatis.servlet;


import cn.school.mybatis.Util.SpringMButil;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.service.StudentService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@WebServlet("/doStudentUpdateServlet")
public class doStudentUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        int stuid=0;
        String sname="";
        String sex="";
        String telephone="";
        String qq="";
        String wechat="";
        int gradeid =0;
        String enterdate="";
        String fromcity="";
        String birthday="";
        String address="";
        String pass="123456";
        String idcardpic="";

        //文件上传
        FileItemFactory item=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(item);
        upload.setHeaderEncoding("utf-8");
        List<FileItem> list= null;
        try {
            list = upload.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        for(FileItem fi:list){
            if(fi.isFormField()){
                //普通的表单元素
//            out.print("<h2>"+fi.getFieldName()+":"+fi.getString("utf-8")+"</h2>");
                switch (fi.getFieldName()){
                    case "stuName":
                        sname=fi.getString("utf-8");
                        break;
                    case "stuSex":
                        sex=fi.getString("utf-8");
                        break;
                    case "stuPhone":
                        telephone=fi.getString("utf-8");
                        break;
                    case "stuQQ":
                        qq=fi.getString("utf-8");
                        break;
                    case "stuWechat":
                        wechat=fi.getString("utf-8");
                        break;
                    case "stuGrade":
                        gradeid=Integer.parseInt(fi.getString("utf-8"));
                        break;
                    case "enterTime":
                        enterdate=fi.getString("utf-8");
                        break;
                    case "stuBirth":
                        birthday=fi.getString("utf-8");
                        break;
                    case "fromCity":
                        fromcity=fi.getString("utf-8");
                        break;
                    case "stuAddr":
                        address=fi.getString("utf-8");
                        break;
                    case "stuid":
                        stuid=Integer.parseInt(fi.getString("utf-8"));
                        break;

                }

            }else{
                //文件
                if (fi.getSize()==0){
//                out.print("<h2>没有文件！</h2>");
                    continue;
                }
//            out.print("<h2>这是一个文件，文件名："+fi.getName()+"，元素名："+fi.getFieldName()+"</h2>");
                String path=this.getServletContext().getRealPath("images");
                //文件要换一个名字,保证后缀是相同
                String fName =fi.getName();
                String houZhui=fName.substring(fName.lastIndexOf("."));
                String fileName= UUID.randomUUID()+houZhui;
                File f=new File(path,fileName);
//            out.print(f.getAbsolutePath());
                idcardpic=fileName;
                try {
                    fi.write(f);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


        Student student = new Student(stuid,sname, sex, telephone, qq, wechat, gradeid, enterdate, fromcity, birthday, address,pass,idcardpic);
//    out.print(student);

        int result = 0;
        try{
            StudentService studentService= SpringMButil.getBean(StudentService.class);
            result = studentService.update(student);
        }catch (Exception e){ out.println("信息修改失败...");e.printStackTrace();}

        if (result==1){
            out.println("<script>alert('"+result+"条信息修改成功！');location.replace('"+req.getContextPath()+"/AdminLTEdemo01/s_student/s_studentInfo.html')</script>");

        }else {
            out.println("<script>alert('信息修改失败...');history.back(-1);</script>");

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
